using System.Diagnostics;
using System.Globalization;
using System.IO;
using Microsoft.Modeling.StoryboardDesigner.Extensions.Common;
using Microsoft.VisualStudio.Modeling.Shell;

namespace Microsoft.Modeling.StoryboardDesigner.Extensions.Features.MoveToNewStoryboard
{
    internal static class Navigator
    {
        /// <summary>
        /// Opens the file specified in this elements "FileName" property.
        /// NB the file must be part of the current project.
        /// </summary>
        internal static void NavigateToStoryboardDiagram(this StoryboardElement storyboardElement)
        {
            // Give up if we don't have a model element or if
            // the filename has not been specified.
            if(storyboardElement == null)
            {
                return;
            }

            if(string.IsNullOrEmpty(storyboardElement.LinkedFile))
            {
                return;
            }

            if(!File.Exists(storyboardElement.LinkedFile))
            {
                PackageUtility.ShowError(storyboardElement.Store,
                    string.Format(
                        CultureInfo.CurrentCulture, 
                        Properties.Resources.FileDoesntExistError,
                        storyboardElement.LinkedFile));
            }
            else
            {
                // Is this a designer file, or something else?
                if(IsDesignerFile(storyboardElement.LinkedFile))
                {
                    // Get a ref to the current DTE object
                    EnvDTE.DTE dte = storyboardElement.Store.GetService<EnvDTE.DTE>();                   

                    Debug.Assert(dte != null, "Error - could not get a ref to the current DTE");

                    // Open the file
                    EnvDTE.ProjectItem prjItem = 
                        dte.Solution.FindProjectItem(storyboardElement.LinkedFile);

                    if(prjItem == null)
                    {
                        PackageUtility.ShowError(storyboardElement.Store,
                            string.Format(
                                CultureInfo.CurrentCulture,
                                Properties.Resources.FileDoesntExistInProjectError,
                                storyboardElement.LinkedFile));
                    }
                    else
                    {
                        if((prjItem.get_IsOpen(EnvDTE.Constants.vsViewKindDesigner) ||
                            prjItem.get_IsOpen(EnvDTE.Constants.vsViewKindAny)) &&
                            prjItem.Document != null)
                        {
                            prjItem.Document.Activate();
                        }
                        else
                        {
                            prjItem.Open(EnvDTE.Constants.vsViewKindPrimary);

                            if(prjItem.Document != null)
                            {
                                prjItem.Document.Activate();
                            }
                        }
                    }
                }
                else
                {
                    Process.Start(storyboardElement.LinkedFile);
                }
            }
        }

        private static bool IsDesignerFile(string fileName)
        {
            return fileName.EndsWith(string.Format(".{0}", Constants.DesignerFileExtension));
        }
    }
}